package com.ysqcould.mp.task;

import cn.hutool.core.util.ObjectUtil;
import com.ysqcould.common.core.domain.entity.JbUser;
import com.ysqcould.common.utils.DateUtils;
import com.ysqcould.common.utils.spring.SpringUtils;
import com.ysqcould.content.domain.Press;
import com.ysqcould.content.mapper.PressMapper;
import com.ysqcould.mp.domain.MpArticle;
import com.ysqcould.mp.service.IMpArticleService;
import com.ysqcould.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

import static com.ysqcould.mp.utils.ParsingArticleUtil.parsingArticles;

/**
 * 定时解析数据库中已经抓取的文章列表
 * @author jcd
 */
@Component
public class TimingTask {

    IMpArticleService service = SpringUtils.getBean(IMpArticleService.class);
    @Resource
    private SysUserMapper userMapper;
    @Resource
    private PressMapper pressMapper;
    /**
     * 获取数据库中未解析的文章地址
     */
    @Scheduled(fixedRate = 90000)
    public void parsingArticle(){
        final List<MpArticle> articles = service.selectArticleList();
        if (ObjectUtil.isEmpty(articles)){
            return;
        }
        articles.forEach(p->{
            Press press = parsingArticles(p.getLink());
            final JbUser user = userMapper.selectUserByName(p.getMpInfoId());
            press.setReleaseTime(p.getCreateTime());
            press.setStatus(0L);
            press.setAsId(user.getId());
            press.setUpdateTime(DateUtils.getNowDate());
            press.setCreateTime(DateUtils.getNowDate());
            final int i = pressMapper.insertPress(press);
            if (i>0){
                p.setParsingStatus(1L);
                service.updateMpArticle(p);
            }
        });
    }
}
